package com.maxuxu.scihome.controller.dept;

import com.maxuxu.scihome.pojo.Department;
import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @className: DepartmentController
 * @author: Maxuxu
 * @date: 2022/7/4 22:15
 * @description: 部门的控制类
 */
@RestController
@Slf4j
public class DepartmentController {
    @Autowired
    Department department;
    @Autowired
    Message message;
    @Autowired
    DepartmentService departmentService;

    @PostMapping("/department/insert")
     public Message insert(HttpServletRequest request){
        String departname = request.getParameter("name");
        String remark = request.getParameter("remark");
        department.setName(departname);
        department.setRemark(remark);
        departmentService.insertDepartment(department);
        message.setJson("插入成功");
        return message;
    }

    @PostMapping("/department/select")
    public Pages findAll(long current, long size) {
        return departmentService.selectPage(current, size);
    }


    @GetMapping("/deparment/removeDept")
    public Message removedepart(HttpServletRequest request) {
        String ids = request.getParameter("ids").trim();
        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s:
                split) {
            Integer target = Integer.valueOf(s);
            list.add(target);
        }
        departmentService.deletdepartment(list);
        message.setJson("删除成功"+ids);
        return message;
    }

    @GetMapping("/department/selectone")
    public Department findone(HttpServletRequest request){
        String name = request.getParameter("name");
        Department department1 = new Department();
        department1.setName(name);
        return departmentService.selectByAny(department1);
    }

    @PostMapping("/department/update")
    public Department update(String old,String name, String remark) {
        log.error("文档的旧标题是：{}，名字是:{},评论是：{}", old, name, remark);
        department.setName(name);
        department.setRemark(remark);
        departmentService.updateDepartmet(old, department);
        return department;
    }

}
